package com.gowants.pems.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gowants.pems.aspect.Importance;
import com.gowants.pems.aspect.OperType;
import com.gowants.pems.aspect.ScmsAopLog;
import com.gowants.pems.config.AccessLimit;
import com.gowants.pems.model.SysUser;
import com.gowants.pems.model.SysLog;
import com.gowants.pems.service.SysUserService;
import com.gowants.pems.service.SysLogService;
import com.gowants.pems.util.DateUtil;
import com.gowants.pems.util.JSONList;
import com.gowants.pems.util.JSONMap;

@RestController
@RequestMapping(value = "/sysLog/")
public class SysLogController extends BaseController {

	@Autowired
	private SysLogService SysLogService;
	@Autowired
	private SysUserService scmsUserService;
	
	/**
	 * 
	 * <获取系统日志列表>
	 * 
	 * @param page  查询的页码
	 * @param limit 查询条数
	 * @Param request 请求对象,用于封装返回信息使用
	 * @return
	 * @remake
	 */
	@RequestMapping(value = "getList", method = RequestMethod.POST)
	@AccessLimit(seconds = 1, maxCount = 3) //1秒内 允许请求3次
	@ScmsAopLog(description = "系统日志列表查看", opType = OperType.execute, importance = Importance.commonly)
	public JSONMap getList(@RequestParam(defaultValue = "1", required = false, value = "page") int page,
			@RequestParam(defaultValue = "10", required = false, value = "limit") int limit,
			@RequestParam(defaultValue = "", required = false, value = "reqData") String reqData,
			HttpServletRequest req) {
		try {
			if (!isPower("/sysLog/getList.do")) {
				return doResponse(req, "TSMS_0000228");
			}
			JSONMap resultMap = new JSONMap();
			SysLog record = new SysLog();
			
			if (!"".equals(reqData)) {
				JSONList jsonCondition = getCondition(reqData);
				for (Object cond : jsonCondition) {
					buildConditionnew(cond, record);
				}
			}
			SysUser userInfo = getUser();  
			Date date = new Date();
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			if (null == record.getStartTime()|| "".equals(record.getStartTime())) {
				String beginTime = dateFormat.format(date).toString() + " 00:00:00";

				record.setStartTime(DateUtil.dateFormat(beginTime));
			}
			if (null == record.getEndTime() || "".equals(record.getEndTime())) {
				String endTime = dateFormat.format(date).toString() + " 23:59:59";
				record.setEndTime(DateUtil.dateFormat(endTime));
			}
			// 设置分页查询信息
			PageHelper.startPage(page, limit);

			List<SysLog> SysLogList = this.SysLogService.select(record);
			PageInfo<SysLog> pageInfo = new PageInfo<SysLog>(SysLogList);

			pageSetData(req, resultMap, page, limit, pageInfo.getPages(), "", SysLogList, pageInfo.getTotal());

			return doResponse(req, "", resultMap);
		} catch (Exception e) {
			return doResponse(req, e.getMessage());
		}
	}

	/**
	 * 
	 * <构建查询条件>
	 * 
	 * @param cond
	 * @param record
	 * @remake
	 */
	private void buildConditionnew(Object cond, SysLog record) {
		JSONMap condition = JSONMap.parseJSONMap(cond.toString());
		if (!this.ckeckBlankKeyWord(condition)) {
			return;
		}
		String field = condition.getString("field");
		String keyword = condition.getString("keyword");

		if (StringUtils.isNotEmpty(field) && StringUtils.isNotEmpty(keyword)) {
			switch (field) {
			case "name":
				record.setName(keyword);
				break;
			case "type":
				record.setType(Integer.parseInt(keyword));
				break;
			case "startTime":
				record.setStartTime(DateUtil.dateFormat(keyword));
				break;
			case "endTime":
				record.setEndTime(DateUtil.dateFormat(keyword));
				break;
			case "importance":
				record.setImportance(Integer.parseInt(keyword));
				break;
			case "userName":
				record.setCreateUname(keyword);
				break;
			}
		}
	}

	/**
	 * 
	 * <空字段校验>
	 * 
	 * @param condition
	 * @return
	 * @remake
	 */
	private boolean ckeckBlankKeyWord(JSONMap condition) {

		if (StringUtils.isBlank(condition.getString("keyword"))) {
			return false;
		}

		return true;
	}
}
